<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Ad;
use App\Models\AdPosition;
use App\Models\AdPosition AS AdPositionModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Ad(['adPosition']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('adPosition.name',"广告位置");
            $grid->column('title');
            $grid->column('descript')->limit(10,"...");
            $grid->img()->image();
            $grid->column('link');
            $grid->column("weapp_link");
            $grid->column('orderby')->editable()->sortable();
//            $grid->column('click_count');
            $grid->column('enabled')->switch();
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            //禁用批量删除
            $grid->disableBatchDelete();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like("title");
                $filter->equal("pid")->select(
                AdPosition::all()->pluck("name","id")->toArray()
                );
                $filter->equal("enabled")->select([
                   0=>"关闭",
                    1=>"开启"
                ]);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Ad(['adPosition']), function (Show $show) {
            $show->field('id');
            $show->field('adPosition.name',"广告位置");
            $show->field('title');
            $show->field('descript');
            $show->img()->image();
            $show->field('link');
            $show->field("weapp_link");
            $show->field('orderby');
            $show->field('click_count');
            $show->field('enabled');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Ad(), function (Form $form) {
            $form->display('id');
            $form->select('pid')->options(
                AdPositionModel::all()->pluck("name","id")->toArray()
            );
            $form->text('title')->required();
            $form->text('descript');
            $form->text('link');
            $form->text("weapp_link");
            $form->image("img")->uniqueName()->autoUpload();
            $form->number('orderby');
            $form->switch('enabled')->default(1);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
